package org.wikinews.service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.wikinews.model.Post;
import org.wikinews.model.Tag;
import org.wikinews.repo.TagRepo;

@Name("postService")
@Scope(ScopeType.STATELESS)
@AutoCreate
public class PostService implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@In
	private TagRepo tagRepo;
	
	public void adicionarListaTagsPost(Post post, String tagsPostTexto) {
		if (tagsPostTexto != null) {
			List<String> listTagsAdicionadas = Arrays.asList(tagsPostTexto.toLowerCase().trim().split(","));
			for (String textoTag : listTagsAdicionadas) {
				Tag tag = tagRepo.getById(textoTag.trim());
				if (tag == null) {
					tag = new Tag(textoTag.trim());
					tagRepo.persist(tag);
				}
				post.addTag(tag);
			}
		}
	}
}
